<?php

namespace App\Listeners;

use App\Events\Collect;
use App\Events\Comment;
use App\Events\Course;
use App\Events\Firm;
use App\Events\Invite;
use App\Events\Like;
use App\Events\Login;
use App\Events\Article;
use App\Events\FirmProfile;
use App\Events\Member;
use App\Events\Message;
use App\Events\Order;
use App\Events\RealName;
use App\Events\Register;
use App\Events\Share;
use App\Events\ToJoin;
use Illuminate\Support\Facades\DB;

class UserEventSubscriber
{

    public function handleUserRegister($event)
    {
        $register_give_points = DB::table('tasks')
                                    ->where('id',13)
                                    ->value('points');

        DB::table('user_tasks')
            ->insert([
                'user_id' => $event->user_id,
                'task_id' => 13,
                'finish_time' => date('Y-m-d')
            ]);

        if ($register_give_points>0){

            $integral_doubling = DB::table('users')
                ->where('users.id',$event->user_id)
                ->join('members','users.member_id','=','members.id')
                ->value('members.integral_doubling');

            if ($integral_doubling>0){
                $register_give_points = $register_give_points*$integral_doubling;
            }

            DB::table('users')
                ->where('id',$event->user_id)
                ->increment('points',$register_give_points);
            DB::table('user_points_details')
                ->insert([
                    'user_id' => $event->user_id,
                    'change_type'=> 1,
                    'points' => $register_give_points,
                    'remark' => '完成注册奖励',
                    'change_time' => date('Y-m-d H:i:s')
                ]);

            event(new Message([
                'user_id' => $event->user_id,
                'type' => 4,
                'content' => '完成注册奖励积分已入账'
            ]));

            $this->upgradeTitle($event->user_id);



        }
    }


    public function handleUserLogin($event)
    {
        $exist = DB::table('users')
                    ->where('id',$event->user_id)
                    ->whereDate('last_login_time',date('Y-m-d'))
                    ->exists();

        if (!$exist){

            DB::table('user_tasks')
                ->insert([
                    'user_id' => $event->user_id,
                    'task_id' => 1,
                    'finish_time' => date('Y-m-d H:i:s')
                ]);

            $login_give_points = DB::table('tasks')
                ->where('id',1)
                ->value('give_points');

            DB::table('users')
                ->where('id',$event->user_id)
                ->update(['last_login_time' => date('Y-m-d H:i:s')]);

            if ($login_give_points>0){

                $integral_doubling = DB::table('users')
                    ->where('users.id',$event->user_id)
                    ->join('members','users.member_id','=','members.id')
                    ->value('members.integral_doubling');

                if ($integral_doubling>0){
                    $login_give_points = $login_give_points*$integral_doubling;
                }
                DB::table('users')
                    ->where('id',$event->user_id)
                    ->increment('points',$login_give_points);
                DB::table('user_points_details')
                    ->insert([
                        'user_id' => $event->user_id,
                        'change_type'=> 1,
                        'points' => $login_give_points,
                        'remark' => '完成登录奖励',
                        'change_time' => date('Y-m-d H:i:s')
                    ]);

                event(new Message([
                    'user_id' => $event->user_id,
                    'type' => 4,
                    'content' => '完成登录奖励积分已入账'
                ]));


                $this->upgradeTitle($event->user_id);



            }
        }


    }

    public function upgradeTitle($user_id)
    {
        $points = DB::table('users')->where('id',$user_id)->value('points');
        $grades = DB::table('user_titles')
                    ->orderByDesc('upgrade_points')
                    ->get();

        foreach ($grades as $grade){
            if ($points>= $grade->upgrade_points){
                DB::table('users')->where('id',$user_id)->update(['user_title_id' => $grade->id]);
                break;
            }
        }
    }

    public function handleUserArticle($event)
    {

        $points = 0;
        $remark = '';
        $task_id = 0;
        switch ($event->article->type){
            case 1 :
                $points = DB::table('tasks')->where('id',8)->value('give_points');
                $task_id = 8;
                $remark = '发布动态奖励';
                break;
            case 4 :
                $points = DB::table('tasks')->where('id',10)->value('give_points');
                $remark = '发布文章奖励';
                $task_id = 10;
                break;
            case 2 :
                $points = DB::table('tasks')->where('id',9)->value('give_points');
                $remark = '发布视频奖励';
                $task_id = 9;
                break;
            case 3 :
                $points = DB::table('tasks')->where('id',9)->value('give_points');
                $remark = '发布视频奖励';
                $task_id = 9;
                break;
            case 5 :
                $points = DB::table('tasks')->where('id',12)->value('give_points');
                $remark = '参与话题讨论奖励';
                $task_id = 12;
                break;
        }

        $exists  = DB::table('user_tasks')
                      ->where([
                          'user_id' => $event->article->user_id,
                          'task_id' => $task_id,
                      ])->whereDate('finish_time',date('Y-m-d H:i:s'))->exists();

        if (!$exists){
            DB::table('user_tasks')
                ->insert([
                    'user_id' => $event->article->user_id,
                    'task_id' => $task_id,
                    'finish_time' => date('Y-m-d H:i:s')
                ]);


            if ($points>0){

                $integral_doubling = DB::table('users')
                    ->where('users.id',$event->article->user_id)
                    ->join('members','users.member_id','=','members.id')
                    ->value('members.integral_doubling');

                if ($integral_doubling>0){
                    $points = $points*$integral_doubling;
                }

                DB::table('users')
                    ->where('id',$event->article->user_id)
                    ->increment('points',$points);
                DB::table('user_points_details')
                    ->insert([
                        'user_id' => $event->article->user_id,
                        'change_type'=> 1,
                        'points' => $points,
                        'remark' => $remark,
                        'change_time' => date('Y-m-d H:i:s')
                    ]);

                event(new Message([
                    'user_id' => $event->article->user_id,
                    'type' => 4,
                    'content' => $remark.'积分已入账'
                ]));

                $this->upgradeTitle($event->article->user_id);

            }

        }




    }


    public function handleUserCollect($event)
    {

        $bool = DB::table('user_tasks')
            ->where('user_id',$event->user_id)
            ->where('task_id',4)
            ->whereDate('finish_time',date('Y-m-d'))
            ->exists();

        if (!$bool){
            $collect_give_points= DB::table('tasks')->where('id',4)->value('give_points');

            DB::table('user_tasks')
                ->insert([
                    'user_id' => $event->user_id,
                    'task_id' => 4,
                    'finish_time' => date('Y-m-d H:i:s')
                ]);
            if ($collect_give_points>0){

                $integral_doubling = DB::table('users')
                    ->where('users.id',$event->user_id)
                    ->join('members','users.member_id','=','members.id')
                    ->value('members.integral_doubling');

                if ($integral_doubling>0){
                    $collect_give_points = $collect_give_points*$integral_doubling;
                }

                DB::table('users')
                    ->where('id',$event->user_id)
                    ->increment('points',$collect_give_points);
                DB::table('user_points_details')
                    ->insert([
                        'user_id' => $event->user_id,
                        'change_type'=> 1,
                        'points' => $collect_give_points,
                        'remark' => '完成收藏奖励',
                        'change_time' => date('Y-m-d H:i:s')
                    ]);

                event(new Message([
                    'user_id' => $event->user_id,
                    'type' => 4,
                    'content' => '完成收藏奖励积分已入账'
                ]));

                $this->upgradeTitle($event->user_id);

            }
        }


    }


    public function handleUserComment($event)
    {

       $bool =  DB::table('user_tasks')
            ->where(  'task_id' , 5)
            ->where('user_id',$event->user_id)
            ->whereDate('finish_time',date('Y-m-d'))
            ->exists();


        if (!$bool){
            $comment_give_points= DB::table('tasks')->where('id',5)->value('give_points');

            DB::table('user_tasks')
                ->insert([
                    'user_id' => $event->user_id,
                    'task_id' => 5,
                    'finish_time' => date('Y-m-d H:i:s')
                ]);

            if ($comment_give_points>0){

                $integral_doubling = DB::table('users')
                    ->where('users.id',$event->user_id)
                    ->join('members','users.member_id','=','members.id')
                    ->value('members.integral_doubling');

                if ($integral_doubling>0){
                    $comment_give_points = $comment_give_points*$integral_doubling;
                }


                DB::table('users')
                    ->where('id',$event->user_id)
                    ->increment('points',$comment_give_points);
                DB::table('user_points_details')
                    ->insert([
                        'user_id' => $event->user_id,
                        'change_type'=> 1,
                        'points' => $comment_give_points,
                        'remark' => '完成评论奖励',
                        'change_time' => date('Y-m-d H:i:s')
                    ]);

                event(new Message([
                    'user_id' => $event->user_id,
                    'type' => 4,
                    'content' => '完成评论奖励积分已入账'
                ]));

                $this->upgradeTitle($event->user_id);

            }

        }


    }


    public function handleUserCourse($event)
    {

        $exist =  DB::table('user_tasks')
            ->where([
                'user_id' => $event->user_id,
                'task_id' => 11,
            ])->whereDate('finish_time',date('Y-m-d'))->exists();

        if (!$exist){
            $study_give_points= DB::table('tasks')->where('id',11)->value('give_points');


            DB::table('user_tasks')
                ->insert([
                    'user_id' => $event->user_id,
                    'task_id' => 11,
                    'finish_time' => date('Y-m-d H:i:s')
                ]);

            if ($study_give_points>0){

                $integral_doubling = DB::table('users')
                    ->where('users.id',$event->user_id)
                    ->join('members','users.member_id','=','members.id')
                    ->value('members.integral_doubling');

                if ($integral_doubling>0){
                    $study_give_points = $study_give_points*$integral_doubling;
                }


                DB::table('users')
                    ->where('id',$event->user_id)
                    ->increment('points',$study_give_points);
                DB::table('user_points_details')
                    ->insert([
                        'user_id' => $event->user_id,
                        'change_type'=> 1,
                        'points' => $study_give_points,
                        'remark' => '完成学习奖励',
                        'change_time' => date('Y-m-d H:i:s')
                    ]);

                event(new Message([
                    'user_id' => $event->user_id,
                    'type' => 4,
                    'content' => '完成学习奖励积分已入账'
                ]));

                $this->upgradeTitle($event->user_id);

            }
        }


    }


    public function handleUserFirm($event)
    {
        $firm_give_points= DB::table('tasks')->where('id',15)->value('give_points');


        DB::table('user_tasks')
            ->insert([
                'user_id' => $event->user_id,
                'task_id' => 15,
                'finish_time' => date('Y-m-d H:i:s')
            ]);
        if ($firm_give_points>0){

            $integral_doubling = DB::table('users')
                ->where('users.id',$event->user_id)
                ->join('members','users.member_id','=','members.id')
                ->value('members.integral_doubling');

            if ($integral_doubling>0){
                $firm_give_points = $firm_give_points*$integral_doubling;
            }

            DB::table('users')
                ->where('id',$event->user_id)
                ->increment('points',$firm_give_points);
            DB::table('user_points_details')
                ->insert([
                    'user_id' => $event->user_id,
                    'change_type'=> 1,
                    'points' => $firm_give_points,
                    'remark' => '认证企业奖励',
                    'change_time' => date('Y-m-d H:i:s')
                ]);

            event(new Message([
                'user_id' => $event->user_id,
                'type' => 4,
                'content' => '认证企业奖励积分已入账'
            ]));


            $this->upgradeTitle($event->user_id);

        }
    }


    public function handleUserFirmProfile($event)
    {
        $firm_profile_give_points= DB::table('tasks')->where('id',17)->value('give_points');

        DB::table('user_tasks')
            ->insert([
                'user_id' => $event->user_id,
                'task_id' => 17,
                'finish_time' => date('Y-m-d H:i:s')
            ]);

        if ($firm_profile_give_points>0){

            $integral_doubling = DB::table('users')
                ->where('users.id',$event->user_id)
                ->join('members','users.member_id','=','members.id')
                ->value('members.integral_doubling');

            if ($integral_doubling>0){
                $firm_profile_give_points = $firm_profile_give_points*$integral_doubling;
            }

            DB::table('users')
                ->where('id',$event->user_id)
                ->increment('points',$firm_profile_give_points);
            DB::table('user_points_details')
                ->insert([
                    'user_id' => $event->user_id,
                    'change_type'=> 1,
                    'points' => $firm_profile_give_points,
                    'remark' => '完成企业资料上传奖励',
                    'change_time' => date('Y-m-d H:i:s')
                ]);

            event(new Message([
                'user_id' => $event->user_id,
                'type' => 4,
                'content' => '完成企业资料上传奖励积分已入账'
            ]));


            $this->upgradeTitle($event->user_id);

        }
    }


    public function handleUserInvite($event)
    {

        $invite_num = DB::table('users')->where('parent_id',$event->user_id)->count('id');

        if ($invite_num==50){
            $invite_give_points= DB::table('tasks')->where('id',23)->value('give_points');

            DB::table('user_tasks')
                ->insert([
                    'user_id' => $event->user_id,
                    'task_id' => 23,
                    'finish_time' => date('Y-m-d H:i:s')
                ]);
            if ($invite_give_points>0){

                $integral_doubling = DB::table('users')
                    ->where('users.id',$event->user_id)
                    ->join('members','users.member_id','=','members.id')
                    ->value('members.integral_doubling');

                if ($integral_doubling>0){
                    $invite_give_points = $invite_give_points*$integral_doubling;
                }

                DB::table('users')
                    ->where('id',$event->user_id)
                    ->increment('points',$invite_give_points);
                DB::table('user_points_details')
                    ->insert([
                        'user_id' => $event->user_id,
                        'change_type'=> 1,
                        'points' => $invite_give_points,
                        'remark' => '完成邀请奖励',
                        'change_time' => date('Y-m-d H:i:s')
                    ]);

                event(new Message([
                    'user_id' => $event->user_id,
                    'type' => 4,
                    'content' => '完成邀请奖励积分已入账'
                ]));


                $this->upgradeTitle($event->user_id);

            }
        }


    }


    public function handleUserLike($event)
    {
        $bool =       DB::table('user_tasks')
            ->where([
                'user_id' => $event->user_id,
                'task_id' => 3,
            ])->whereDate('finish_time',date('Y-m-d'))->exists();

        if (!$bool){
            $points= DB::table('tasks')->where('id',3)->value('give_points');

            DB::table('user_tasks')
                ->insert([
                    'user_id' => $event->user_id,
                    'task_id' => 3,
                    'finish_time' => date('Y-m-d H:i:s')
                ]);
            if ($points>0){

                $integral_doubling = DB::table('users')
                    ->where('users.id',$event->user_id)
                    ->join('members','users.member_id','=','members.id')
                    ->value('members.integral_doubling');

                if ($integral_doubling>0){
                    $points = $points*$integral_doubling;
                }

                DB::table('users')
                    ->where('id',$event->user_id)
                    ->increment('points',$points);
                DB::table('user_points_details')
                    ->insert([
                        'user_id' => $event->user_id,
                        'change_type'=> 1,
                        'points' => $points,
                        'remark' => '完成点赞奖励',
                        'change_time' => date('Y-m-d H:i:s')
                    ]);

                event(new Message([
                    'user_id' => $event->user_id,
                    'type' => 4,
                    'content' => '完成点赞奖励积分已入账'
                ]));


                $this->upgradeTitle($event->user_id);
            }
        }


    }


    public function handleUserMember($event)
    {

        $remark= '';
        $task_id = 0;
        $points = 0;
        $user = DB::table('users')->where('id',$event->user_id)->first();
        switch ($user->member_id){
            case 1:
                $points= DB::table('tasks')->where('id',18)->value('give_points');
                $task_id= 18;
                $remark ='成为白银会员奖励';
                break;

            case 2:
                $points= DB::table('tasks')->where('id',19)->value('give_points');
                $task_id= 19;
                $remark ='成为黄金会员奖励';
                break;

            case 3:
                $points= DB::table('tasks')->where('id',20)->value('give_points');
                $task_id= 20;
                $remark ='成为钻石会员奖励';
                break;
        }


        DB::table('user_tasks')
            ->insert([
                'user_id' => $event->user_id,
                'task_id' => $task_id,
                'finish_time' => date('Y-m-d H:i:s')
            ]);
        if ($points>0){


            $integral_doubling = DB::table('users')
                ->where('users.id',$event->user_id)
                ->join('members','users.member_id','=','members.id')
                ->value('members.integral_doubling');

            if ($integral_doubling>0){
                $points = $points*$integral_doubling;
            }

            DB::table('users')
                ->where('id',$event->user_id)
                ->increment('points',$points);
            DB::table('user_points_details')
                ->insert([
                    'user_id' => $event->user_id,
                    'change_type'=> 1,
                    'points' => $points,
                    'remark' => $remark,
                    'change_time' => date('Y-m-d H:i:s')
                ]);

            event(new Message([
                'user_id' => $event->user_id,
                'type' => 4,
                'content' => $remark.'积分已入账'
            ]));


            $this->upgradeTitle($event->user_id);

        }
    }


    public function handleUserOrder($event)
    {


        $bool =  DB::table('user_tasks')
                    ->where('user_id',$event->user_id)
                    ->whereDate('finish_time',date('Y-m-d'))
                    ->where('task_id',7)
                    ->exists();

        if (!$bool){
            $points= DB::table('tasks')->where('id',7)->value('give_points');

            DB::table('user_tasks')
                ->insert([
                    'user_id' => $event->user_id,
                    'task_id' => 7,
                    'finish_time' => date('Y-m-d H:i:s')
                ]);
            if ($points>0){


                $integral_doubling = DB::table('users')
                    ->where('users.id',$event->user_id)
                    ->join('members','users.member_id','=','members.id')
                    ->value('members.integral_doubling');

                if ($integral_doubling>0){
                    $points = $points*$integral_doubling;
                }

                DB::table('users')
                    ->where('id',$event->user_id)
                    ->increment('points',$points);
                DB::table('user_points_details')
                    ->insert([
                        'user_id' => $event->user_id,
                        'change_type'=> 1,
                        'points' => $points,
                        'remark' => '完成下单奖励',
                        'change_time' => date('Y-m-d H:i:s')
                    ]);

                event(new Message([
                    'user_id' => $event->user_id,
                    'type' => 4,
                    'content' => '完成下单奖励积分已入账'
                ]));

                $this->upgradeTitle($event->user_id);

            }
        }



    }


    public function handleUserRealName($event)
    {
        $points= DB::table('tasks')->where('id',14)->value('give_points');


        DB::table('user_tasks')
            ->insert([
                'user_id' => $event->user_id,
                'task_id' => 14,
                'finish_time' => date('Y-m-d H:i:s')
            ]);
        if ($points>0){

            $integral_doubling = DB::table('users')
                ->where('users.id',$event->user_id)
                ->join('members','users.member_id','=','members.id')
                ->value('members.integral_doubling');

            if ($integral_doubling>0){
                $points = $points*$integral_doubling;
            }

            DB::table('users')
                ->where('id',$event->user_id)
                ->increment('points',$points);
            DB::table('user_points_details')
                ->insert([
                    'user_id' => $event->user_id,
                    'change_type'=> 1,
                    'points' => $points,
                    'remark' => '个人实名认证奖励',
                    'change_time' => date('Y-m-d H:i:s')
                ]);

            event(new Message([
                'user_id' => $event->user_id,
                'type' => 4,
                'content' => '个人实名认证奖励积分已入账'
            ]));

            $this->upgradeTitle($event->user_id);

        }
    }

    public function handleUserShare($event)
    {

        $bool = DB::table('user_tasks')
            ->where('user_id',$event->user_id)
            ->whereDate('finish_time',date('Y-m-d'))
            ->where('task_id',6)
            ->exists();

        if (!$bool){
            $points= DB::table('tasks')->where('id',6)->value('give_points');

            DB::table('user_tasks')
                ->insert([
                    'user_id' => $event->user_id,
                    'task_id' => 6,
                    'finish_time' => date('Y-m-d H:i:s')
                ]);

            if ($points>0){

                $integral_doubling = DB::table('users')
                    ->where('users.id',$event->user_id)
                    ->join('members','users.member_id','=','members.id')
                    ->value('members.integral_doubling');

                if ($integral_doubling>0){
                    $points = $points*$integral_doubling;
                }

                DB::table('users')
                    ->where('id',$event->user_id)
                    ->increment('points',$points);
                DB::table('user_points_details')
                    ->insert([
                        'user_id' => $event->user_id,
                        'change_type'=> 1,
                        'points' => $points,
                        'remark' => '分享奖励',
                        'change_time' => date('Y-m-d H:i:s')
                    ]);

                event(new Message([
                    'user_id' => $event->user_id,
                    'type' => 4,
                    'content' => '分享奖励积分已入账'
                ]));
                $this->upgradeTitle($event->user_id);

            }
        }


    }


    public function handleUserToJoin($event)
    {
        $points=  DB::table('tasks')->where('id',16)->value('give_points');;

        DB::table('user_tasks')
            ->insert([
                'user_id' => $event->user_id,
                'task_id' => 16,
                'finish_time' => date('Y-m-d H:i:s')
            ]);

        if ($points>0){

            $integral_doubling = DB::table('users')
                ->where('users.id',$event->user_id)
                ->join('members','users.member_id','=','members.id')
                ->value('members.integral_doubling');

            if ($integral_doubling>0){
                $points = $points*$integral_doubling;
            }

            DB::table('users')
                ->where('id',$event->user_id)
                ->increment('points',$points);
            DB::table('user_points_details')
                ->insert([
                    'user_id' => $event->user_id,
                    'change_type'=> 1,
                    'points' => $points,
                    'remark' => '制定加盟计划奖励',
                    'change_time' => date('Y-m-d H:i:s')
                ]);

            event(new Message([
                'user_id' => $event->user_id,
                'type' => 4,
                'content' => '制定加盟计划奖励积分已入账'
            ]));

            $this->upgradeTitle($event->user_id);

        }
    }


    public function subscribe($events)
    {
        $events->listen(
            Register::class,
            [UserEventSubscriber::class, 'handleUserRegister']
        );


        $events->listen(
            Login::class,
            [UserEventSubscriber::class, 'handleUserLogin']
        );
        $events->listen(
            Article::class,
            [UserEventSubscriber::class, 'handleUserArticle']
        );

        $events->listen(
            Collect::class,
            [UserEventSubscriber::class, 'handleUserCollect']
        );

        $events->listen(
            Comment::class,
            [UserEventSubscriber::class, 'handleUserComment']
        );


        $events->listen(
            Course::class,
            [UserEventSubscriber::class, 'handleUserCourse']
        );

        $events->listen(
            Firm::class,
            [UserEventSubscriber::class, 'handleUserFirm']
        );

        $events->listen(
            FirmProfile::class,
            [UserEventSubscriber::class, 'handleUserFirmProfile']
        );


        $events->listen(
            Invite::class,
            [UserEventSubscriber::class, 'handleUserInvite']
        );


        $events->listen(
            Like::class,
            [UserEventSubscriber::class, 'handleUserLike']
        );


        $events->listen(
            Member::class,
            [UserEventSubscriber::class, 'handleUserMember']
        );



        $events->listen(
            Order::class,
            [UserEventSubscriber::class, 'handleUserOrder']
        );



        $events->listen(
            RealName::class,
            [UserEventSubscriber::class, 'handleUserRealName']
        );


        $events->listen(
            Share::class,
            [UserEventSubscriber::class, 'handleUserShare']
        );


        $events->listen(
            ToJoin::class,
            [UserEventSubscriber::class, 'handleUserToJoin']
        );

    }
}
